// pages/index1/index1.js

var app = getApp()
Page({
  /**
   * 页面的初始数据 
   */
  data: {
    loading: false,
    plain: false,
    disabled: false,//按钮一旦点击提交 则不能操作
    wxid: '',//微信id
    scindex: 0,//当前选中的学校编号
    schs: [],//所有校区
    sid: '',//儿童主键id
    sname: '',//儿童姓名
    sfzh: '',//儿童身份证号
    jzxm: '',//家长姓名
    jzsj: '',//家长手机
    csid: '',//校区id
    hjszd: '',//户籍所在地

    djson: {},//登记提交需要提交的所有资料

    imgurls: [],//已经上传了的图片
    newimage: [],//新上传的图片
    xturl: app.myUlife.ulifeUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    //获取传递的参数
    this.setData({
      sid: options.sid
    })

    that.getdata();
    var wxid = wx.getStorageSync('openid');
    this.setData({
      wxid: wxid
    })
  },


  getdata: function () {//定义函数名称
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/studentCamArea.html',//请求地址
      data: {//发送给后台的数据
        sid: that.data.sid
      },
      header: {//请求头
        "Content-Type": "applciation/json"
      },
      method: "GET",//get为默认方法/POST
      success: function (res) {
        var urls = res.data.imgurls;
        var imgurls = [];
        for (var k = 0; k < urls.length; k++) {
          imgurls.push(urls[k].IMAGEURL);
        }

        that.setData({
          sname: res.data.SNAME,//儿童姓名
          hjszd: res.data.HJSZD,//户籍所在地
          jzxm: res.data.JZXM,//家长姓名
          jzsj: res.data.JZSJ,//家长手机
          csid: res.data.CSID,//学校id
          sfzh: res.data.SFZH,//身份证号
          imgurls: imgurls
        });
        var scindex = 0;
        var schs = [{ CSID: '', CSNAME: '请选择学校' }];
        for (var i = 0; i < res.data.cam.length; i++) {
          var sch = res.data.cam[i];
          if (sch.CSID == res.data.CSID) {
            scindex = i + 1;
          }
          schs.push(sch);
        }
        that.setData({
          schs: schs,
          scindex: scindex,
        });
        wx.hideLoading()
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },


  //选中学校
  bindPickerChange: function (e) {
    this.setData({
      scindex: e.detail.value
    })
  },
  //获取儿童姓名
  snameinput: function (e) {
    this.setData({
      sname: e.detail.value
    })
  },

  //获取儿童身份证号码
  sfzinput: function (e) {
    this.setData({
      sfzh: e.detail.value
    })
  },
  //家长姓名
  jzxminput: function (e) {
    this.setData({
      jzxm: e.detail.value
    })
  },
  //家长手机号
  jzsjhinput: function (e) {
    this.setData({
      jzsj: e.detail.value
    })
  },

  //户籍所在地
  hjszdinput: function (e) {
    this.setData({
      hjszd: e.detail.value
    })
  },

  //上传图片
  uploadimage: function () {
    var that = this;
    wx.chooseImage({
      count: 4, //张数， 默认9
      sizeType: ['compressed'], //建议压缩图
      sourceType: ['album', 'camera'], // 来源是相册、相机
      success: function (res) {
        var newimage = that.data.newimage;
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          newimage.push(res.tempFilePaths[i]);
        }
        that.setData({
          newimage: newimage,
        })
      }
    });
  },


  //删除图片
  delImage: function (e) {
    var cuindex = e.target.dataset.index;
    var type = e.target.dataset.type;
    var imgurls = this.data.imgurls;//老图片
    var newimage = this.data.newimage;//新图片
    if (type == "old") {
      imgurls.splice(cuindex, 1);
      this.setData({
        imgurls: imgurls,
      })
    } else {
      newimage.splice(cuindex, 1);
      this.setData({
        newimage: newimage,
      })
    }
  },


  //预览图片
  viewimage: function (e) {
    var cuindex = e.target.dataset.index;
    var type = e.target.dataset.type;
    var xturl = this.data.xturl;
    //老图片
    var imgurls = this.data.imgurls;
    var urls = [];
    for (var k = 0; k < imgurls.length; k++) {
      urls.push(xturl + imgurls[k]);
    }
    //新图片
    var newimage = this.data.newimage;
    for (var j = 0; j < newimage.length; j++) {
      urls.push(newimage[j]);
    }
    //当前图片
    var nimage = "";
    if (type == "old") {
      nimage = urls[cuindex];
    } else {
      nimage = newimage[cuindex];
    }

    wx.previewImage({
      current: nimage, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },


  //登记提交
  savestudent: function (e) {
    var schs = this.data.schs;
    var scindex = this.data.scindex;
    var csid = schs[scindex].CSID;
    if (csid == '') {
      wx.showModal({
        title: '友情提示',
        content: '请选择你需要登记的学校!',
        showCancel: !1,
      })
      return;
    }

    var csnum = schs[scindex].CSNUM;
    var sname = this.data.sname;
    if (sname == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入儿童姓名!',
        showCancel: !1,
      })
      return;
    }

    var sfzh = this.data.sfzh;
    if (sfzh == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入儿童身份证号!',
        showCancel: !1,
      })
      return;
    }

    var jzxm = this.data.jzxm;
    if (jzxm == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入家长姓名!',
        showCancel: !1,
      })
      return;
    }

    var jzsj = this.data.jzsj;
    if (jzsj == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入家长手机!',
        showCancel: !1,
      })
      return;
    }

    var hjszd = this.data.hjszd;
    if (hjszd == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输户籍所在地!',
        showCancel: !1,
      })
      return;
    }

    wx.showLoading({
      title: '保存中',
    })

    //先上传图片
    var newimage = this.data.newimage;
    if (newimage.length > 0) {
      //先上传图片
      this.uploadStuurl();
    } else {
      this.oksavestudent();
    }
  },



  uploadStuurl: function () {
    var that = this;
    var newimage = this.data.newimage;
    var imgurls = this.data.imgurls;
    const uploadTask = wx.uploadFile({
      url: app.myUlife.ulifeUrl + '/wechart/uploadBmurl.html',
      filePath: newimage[0],
      name: 'file',
      formData: {},
      success: function (res) {
        var img = JSON.parse(res.data);
        var url = img.urls;
        imgurls.push(url);
        that.setData({
          imgurls: imgurls
        });
        that.data.newimage.splice(0, 1);
        if (that.data.newimage.length > 0) {
          that.uploadStuurl();
        } else {
          that.oksavestudent();
        }
      }
    })
  },


  oksavestudent: function (e) {
    var schs = this.data.schs;
    var scindex = this.data.scindex;
    var csid = schs[scindex].CSID;
    var csnum = schs[scindex].CSNUM;
    var sname = this.data.sname;
    var sfzh = this.data.sfzh;
    var jzxm = this.data.jzxm;
    var jzsj = this.data.jzsj;
    var hjszd = this.data.hjszd;
    
    var urls = this.data.imgurls;
    var that = this;
    var djson = {
      sid: this.data.sid,
      csid: csid,
      csido: this.data.csid,
      csnum: csnum,
      sname: sname,
      sfzh: sfzh,
      jzxm: jzxm,
      jzsj: jzsj,
      hjszd: hjszd,
      urls: urls,
      tsrc: "1",
    };

    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/updateStudent.html',//请求地址
      data: djson,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.result) {
          wx.redirectTo({
            url: '../../pages/bmsuccess/bmsuccess'
          })
        } else {
          wx.showModal({
            title: '友情提示',
            content: res.data.msg,
            showCancel: !1,
          })
        }

      }
    })
  }

})
